package br.com.orion.sga.controller;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.NavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.json.JSONObject;

import br.com.orion.sga.service.GeneratePaymentReport;
import br.com.orion.sga.to.MonthPaymentReport;
import br.com.orion.sga.utils.HttpRequestUtil;

@ManagedBean(name="relatorioPagamentoController")
@ViewScoped
public class RelatorioPagamentoController implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Date initalDate;
	private Date endDate;
	private String initialDateToShow;
	private String endDateToShow;
	private LinkedHashMap<String,MonthPaymentReport> reportData;
	private List<String> titlesOfTheTables;
	private List<String> monthToPlotOnChart;
	private LinkedHashMap<String,ArrayList<String>> daysValuesToPlotOnChart;
	private JSONObject dataToChart;
	
	@ManagedProperty("#{generatePaymentReport}")
	private GeneratePaymentReport generatePaymentReport;
	
	@PostConstruct
	private void init() {
		getFilterInformation();
		
		if(initalDate!=null && endDate!=null){
			DateFormat formatter; 
		    formatter = new SimpleDateFormat("dd-MM-yyyy");
			initialDateToShow = formatter.format(initalDate.getTime());
			endDateToShow = formatter.format(endDate.getTime());
			generatePaymentReport();
		}
	}
	
	public void generatePaymentReport(){
		reportData = generatePaymentReport.generatePaymentReport(initalDate, endDate);
		titlesOfTheTables = new ArrayList<String>();
		
		monthToPlotOnChart = new ArrayList<String>();
		daysValuesToPlotOnChart = new LinkedHashMap<String, ArrayList<String>>();
		String valueDay = null;
		
		/*Isto é para pegar todos os dias encontrados para preenhcer o header
		 *da tabela*/
		for(String key : reportData.keySet()){
			MonthPaymentReport data = reportData.get(key);
			/*Isto é para o chart*/
			monthToPlotOnChart.add(key);
			for(Integer day : data.getPaymentDays().keySet()){
				if(!titlesOfTheTables.contains("Dia-"+day)){
					titlesOfTheTables.add("Dia-"+day);
				}
				valueDay = data.getPaymentDays().get(day);
				/*Isto é para o chart*/
				putValuesToDayForAllMonth(valueDay, day);
			}
		}
		System.out.println("teste");
		dataToChart = new JSONObject(daysValuesToPlotOnChart);
	}

	private void putValuesToDayForAllMonth(String valueDay, Integer day) {
		if(valueDay.equals("-")){
			valueDay = "0";
		}
		if(daysValuesToPlotOnChart.get("Dia-"+day)== null){
			daysValuesToPlotOnChart.put("Dia-"+day, new ArrayList<String>());
		}
		daysValuesToPlotOnChart.get("Dia-"+day).add(valueDay);
	}
	
	public void generateChart(){
		
	}
	
	public void showReport(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(initalDate);
		
		SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
		String initalDate = format.format(cal.getTime());
		
		cal.setTime(endDate);
		String endDate = format.format(cal.getTime());
		
		FacesContext context = FacesContext.getCurrentInstance();
	    NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
	    navigationHandler.handleNavigation(context, null, "/pages/showPaymentReport.jsf?faces-redirect=true&amp;initalDate="+initalDate+"&amp;endDate="+endDate);
	}
	
	public void getFilterInformation(){
		Object initialDate = HttpRequestUtil.getParameterValueInRequest("initalDate");
		Object endDate	   = HttpRequestUtil.getParameterValueInRequest("endDate");
		
		String dataInicial = (String) initialDate;
		String dataFinal   = (String) endDate;
		
	      DateFormat formatter; 
	      formatter = new SimpleDateFormat("dd-MM-yyyy");
		
		try {
			if (initialDate != null) {
				this.initalDate = (Date)formatter.parse(dataInicial);
			}else{
				this.initalDate = null;
			}
			
			if(endDate != null){
				this.endDate = (Date)formatter.parse(dataFinal);;
			}else{
				this.endDate = null;
			}
			
		} catch (NumberFormatException e) {
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	

	public List<String> getMonthToPlotOnChart() {
		return monthToPlotOnChart;
	}

	public void setMonthToPlotOnChart(List<String> monthToPlotOnChart) {
		this.monthToPlotOnChart = monthToPlotOnChart;
	}

	public LinkedHashMap<String, ArrayList<String>> getDaysValuesToPlotOnChart() {
		return daysValuesToPlotOnChart;
	}

	public void setDaysValuesToPlotOnChart(
			LinkedHashMap<String, ArrayList<String>> daysValuesToPlotOnChart) {
		this.daysValuesToPlotOnChart = daysValuesToPlotOnChart;
	}

	public Date getInitalDate() {
		return initalDate;
	}

	public void setInitalDate(Date initalDate) {
		this.initalDate = initalDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getInitialDateToShow() {
		return initialDateToShow;
	}

	public void setInitialDateToShow(String initialDateToShow) {
		this.initialDateToShow = initialDateToShow;
	}

	public String getEndDateToShow() {
		return endDateToShow;
	}

	public void setEndDateToShow(String endDateToShow) {
		this.endDateToShow = endDateToShow;
	}

	public LinkedHashMap<String,MonthPaymentReport> getReportData() {
		return reportData;
	}

	public void setReportData(LinkedHashMap<String,MonthPaymentReport> reportData) {
		this.reportData = reportData;
	}

	public List<String> getTitlesOfTheTables() {
		return titlesOfTheTables;
	}


	public void setTitlesOfTheTables(List<String> titlesOfTheTables) {
		this.titlesOfTheTables = titlesOfTheTables;
	}


	public GeneratePaymentReport getGeneratePaymentReport() {
		return generatePaymentReport;
	}


	public void setGeneratePaymentReport(GeneratePaymentReport generatePaymentReport) {
		this.generatePaymentReport = generatePaymentReport;
	}

	public JSONObject getDataToChart() {
		return dataToChart;
	}

	public void setDataToChart(JSONObject dataToChart) {
		this.dataToChart = dataToChart;
	}
	
	
	
}
